package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    @Autowired
    PanelConverter panelConverter;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();
        PanelDto panelDto = new PanelDto();

        try {
            //获取主页板块信息
            List<Panel>panels = panelMapper.getAllPanelInfo();
            for (Panel panel : panels) {
                List<PanelContentItem>panelContentItemList=panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                panel.setPanelContentItems(panelContentItemList);
                //对象转化
               List<PanelContentItemDto>panelContentItems=contentConverter.panelContentItem2Dto(panelContentItemList);
               panelDto.setPanelContentItems(panelContentItems);
            }
            List<PanelDto>panelDtos=  panelConverter.panelsDoToDto(panels);
            homePageResponse.setPanelContentItemDtos(panelDtos);
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            homePageResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            homePageResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return homePageResponse;


//        List<Panel> allPanelInfo = mapper.getAllPanelInfo();
//
//        HomePageResponse response = new HomePageResponse();
//        if (allPanelInfo == null || allPanelInfo.size() == 0) {
//            return response;
//        }
 //       Set<PanelDto> result = allPanelInfo.stream().map(x -> converter.panen2Dto(x)).collect(Collectors.toSet());
//        response.setPanelContentItemDtos(result);
//        return response;
    }
}
